//
//  AiyouDataBase.m
//  Lovelife
//
//  Created by dllo on 15/5/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AiyouDataBase.h"

@implementation AiyouDataBase
+ (instancetype)getDataBase
{
    static AiyouDataBase *aiyouDB = nil;
    if (aiyouDB == nil) {
        aiyouDB = [[AiyouDataBase alloc]init];
    }
    return aiyouDB;
}
static sqlite3 *aiyouDataBase = nil;
- (void)openDB
{
    if (aiyouDataBase != nil) {
        NSLog(@"数据打开");
    }
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"aiyou.sqlite"];
    NSLog(@"%@",filePath);
    int result = sqlite3_open(filePath.UTF8String, &aiyouDataBase);
    
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
        // 创建数据库语句

    NSString *createSql = @"CREATE TABLE IF NOT EXISTS country(countryId INTEGER PRIMARY KEY AUTOINCREMENT, cnname TEXT, enname TEXT, count TEXT, label TEXT, photo TEXT, flag INT, hot TEXT, continentId TEXT)";

        int result = sqlite3_exec(aiyouDataBase, createSql.UTF8String, NULL, NULL, nil);
        if (result == SQLITE_OK) {
            NSLog(@"创建成功");
            
        }else{
            NSLog(@"创建失败");
        }
        
    }else{
        NSLog(@"打开失败");
    }
    
}


- (void)insertCountry:(CountryModel *)country;
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO country(countryId, cnname, enname, count, label, photo, flag, hot, continentId) values('%@', '%@', '%@', '%@', '%@', '%@', '%ld', '%hhd', '%@')",country.countryId,country.cnname,country.enname,country.count,country.label, country.photo, (long)country.flag,country.ifHot,country.continentId];
    int result = sqlite3_exec(aiyouDataBase, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }else{
        NSLog(@"添加失败");
    }
    
}

-(NSMutableArray *)selectCountry:(BOOL)isHot continentId:(NSString *)continentId
{
    if (aiyouDataBase != nil) {
        NSLog(@"数据库已打开");
    }
    NSMutableArray *array = [NSMutableArray array];
    NSString *selectSql = [NSString stringWithFormat:@"SELECT * FROM country Where hot = %hhd and continentId = %@",isHot,continentId];

    // 声明数据库跟随指针, 跟踪表里的每一行
    sqlite3_stmt *stmt = nil;
    // 查询之前 需要将数据库对象db, sql 语句, stmt关联
    // 参数2 sql语句 参数3 sql语句长度, -1为不限制长度
    int result = sqlite3_prepare_v2(aiyouDataBase, selectSql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *countryId = sqlite3_column_text(stmt, 0);
            const unsigned char *cnname = sqlite3_column_text(stmt, 1);
            const unsigned char *enname = sqlite3_column_text(stmt, 2);
            const unsigned char *count = sqlite3_column_text(stmt, 3);
            const unsigned char *label = sqlite3_column_text(stmt, 4);
            const unsigned char *photo = sqlite3_column_text(stmt, 5);
            int flag = sqlite3_column_int(stmt, 6);
            const unsigned char *ifHot = sqlite3_column_text(stmt, 7);
            const unsigned char *continentId = sqlite3_column_text(stmt, 8);

            CountryModel *country = [[CountryModel alloc]init];
            country.countryId = [NSString stringWithUTF8String:(const char *) countryId];
            country.cnname = [NSString stringWithUTF8String:(const char *)cnname];
            country.enname = [NSString stringWithUTF8String:(const char *)enname];
            country.count = [NSString stringWithUTF8String:(const char *)count];
            country.label = [NSString stringWithUTF8String:(const char *)label];
            country.flag = flag;
            country.photo = [NSString stringWithUTF8String:(const char *)photo];
            country.ifHot = [[NSString stringWithUTF8String:(const char *)ifHot] boolValue];
            country.continentId = [NSString stringWithUTF8String:(const char *)continentId];
            [array addObject:country];
        }
    }else {
        NSLog(@"查询失败");
    }
    return array;
}

- (void)closeDB
{
    int result = sqlite3_close(aiyouDataBase);
    if (result == SQLITE_OK) {
        NSLog(@"关闭成功");
    }else{
        NSLog(@"关闭失败");
    }
}
@end
